import 'package:flutter/material.dart';
import 'package:intl/date_symbol_data_local.dart';
import 'package:intl/intl.dart';
import 'package:stack_study/models/LoginResponseData.dart';

import '../utils/get_dayOf_lessons.dart';
import '../utils/class_calendar.dart';

///首页
///李环、常龙洁创建
///吕佳伟实现数据动态化
class Index extends StatefulWidget {
  Index(Object arguments, {Key key}) : super(key: key);
  @override
  _IndexState createState() => _IndexState();
}

class _IndexState extends State<Index> {
  Future<List<Widget>> _temps;
  LoginResponseData userData;
  String _studentId;
  String _token;
  @override
  void initState() {
    super.initState();
    _temps = _getClassItems();
  }

  void _initData(context) {
    setState(() {
      userData = ModalRoute.of(context).settings.arguments;
      _studentId = userData.data.user_id;
      _token = userData.token;
    });
  }

  var _dates = initDate();
  void _getDates(value) {
    _dates = DateFormat('EEE', "en_US").format(value).toString();
    setState(() {
      _temps = _getClassItems();
    });
  }

  Future<List<Widget>> _getClassItems() async {
    List<Widget> classItems =
        await getOneDaylessons(this._dates, this._studentId, this._token);
    if (classItems.isEmpty) {
      return [
        Container(
          height: 60,
          child: Center(
            child: Text("今日暂无课程"),
          ),
        )
      ];
    } else {
      return classItems;
    }
  }

  @override
  Widget build(BuildContext context) {
    _initData(context);
    return Scaffold(
      appBar: AppBar(
        title: Text("课程"),
      ),
      body: Container(
        padding: EdgeInsets.all(8),
        child: Column(
          children: <Widget>[
            // header
            Container(
              child: Row(
                children: <Widget>[
                  Expanded(
                    child: Row(
                      children: [
                        Icon(Icons.account_circle_rounded,
                            color: Colors.orange[100]),
                        Text(
                          userData.data.name, //username
                          style: TextStyle(
                            color: Colors.grey,
                          ),
                        ),
                      ],
                    ),
                    flex: 3,
                  ),
                  Expanded(
                    child: Row(
                      children: [
                        ElevatedButton(
                          child: Text(
                            '退出登录',
                            style: TextStyle(
                              color: Colors.white,
                            ),
                          ),
                          style: ButtonStyle(
                              backgroundColor:
                                  MaterialStateProperty.all(Colors.blue[200]),
                              alignment: Alignment.center),
                          onPressed: () =>
                              {Navigator.of(context).pushNamed("/")},
                        ),
                      ],
                    ),
                    flex: 1,
                  )
                ],
              ),
            ),
            // content
            new Expanded(
              child: ListView(
                // shrinkWrap: true,
                children: <Widget>[
                  //calendar_class
                  CalendarForClass(
                    getDates: _getDates,
                  ),
                  Divider(
                    height: 10,
                    thickness: 1,
                    color: Colors.grey,
                    indent: 10,
                    endIndent: 20,
                  ),
                  //当日课程
                  Container(
                    child: FutureBuilder(
                        future: _temps,
                        builder:
                            // ignore: missing_return
                            (BuildContext context, AsyncSnapshot snapshot) {
                          if (snapshot.connectionState ==
                              ConnectionState.done) {
                            if (snapshot.hasError) {
                              return Text(snapshot.error.toString());
                            }
                            return Column(children: snapshot.data);
                          }
                          return Center(
                            heightFactor: 130.00,
                            widthFactor: 130.00,
                            child: CircularProgressIndicator(),
                          );
                        }),
                  ),
                  Divider(
                    height: 10,
                    thickness: 1,
                    color: Colors.grey,
                    indent: 10,
                    endIndent: 20,
                  ),
                  Row(
                    children: <Widget>[
                      Text(
                        '学习中心',
                        style: TextStyle(
                          color: Colors.orange,
                          fontSize: 20,
                        ),
                      ),
                    ],
                  ),
                  SizedBox(
                    height: 20,
                  ),
                  Container(
                    child: Row(
                      children: [
                        Column(
                          children: [
                            Icon(
                              MyIcons.course,
                              color: Colors.brown[200],
                              size: 70,
                            ),
                            SizedBox(
                              height: 5,
                            ),
                            Text("我的课程"),
                          ],
                        ),
                        SizedBox(
                          width: 60,
                        ),
                        Column(
                          children: [
                            Icon(
                              MyIcons.analysis,
                              color: Colors.green[100],
                              size: 70,
                            ),
                            SizedBox(
                              height: 5,
                            ),
                            Text("学情分析"),
                          ],
                        ),
                        SizedBox(
                          width: 60,
                        ),
                        Column(
                          children: [
                            Icon(
                              MyIcons.favor,
                              color: Colors.pink[100],
                              size: 70,
                            ),
                            SizedBox(
                              height: 5,
                            ),
                            Text("我的收藏"),
                          ],
                        ),
                      ],
                    ),
                  ),
                  SizedBox(
                    height: 50,
                  ),
                  Divider(
                    height: 10,
                    thickness: 1,
                    color: Colors.grey,
                    indent: 10,
                    endIndent: 20,
                  ),
                  Row(
                    children: <Widget>[
                      Text(
                        '个人中心',
                        style: TextStyle(
                          color: Colors.orange,
                          fontSize: 20,
                        ),
                      ),
                    ],
                  ),
                  SizedBox(
                    height: 10,
                  ),
                  Container(
                    child: Row(
                      children: [
                        Column(
                          children: [
                            Icon(
                              MyIcons.info,
                              color: Colors.purple[200],
                              size: 70,
                            ),
                            SizedBox(
                              height: 5,
                            ),
                            Text("个人资料"),
                          ],
                        ),
                        SizedBox(
                          width: 60,
                        ),
                        Column(
                          children: [
                            Icon(
                              MyIcons.password,
                              color: Colors.blue[100],
                              size: 70,
                            ),
                            SizedBox(
                              height: 5,
                            ),
                            Text("修改密码"),
                          ],
                        ),
                      ],
                    ),
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}

class MyIcons {
  static const IconData course =
      const IconData(0xe62a, fontFamily: "myIcons", matchTextDirection: true);
  static const IconData analysis =
      const IconData(0xe634, fontFamily: "myIcons", matchTextDirection: true);
  static const IconData favor =
      const IconData(0xee21, fontFamily: "myIcons", matchTextDirection: true);
  static const IconData info =
      const IconData(0xe626, fontFamily: "myIcons", matchTextDirection: true);
  static const IconData password =
      const IconData(0xe64d, fontFamily: "myIcons", matchTextDirection: true);
}

String initDate() {
  initializeDateFormatting();
  return DateFormat('EEE', "en_US").format(DateTime.now()).toString();
}
